package com.ruoyi.operation.order.wwebchat.service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.operation.order.wwebchat.domain.Wwebchat;
import com.ruoyi.operation.order.wwebchat.mapper.WwebchatMapper;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WwebchatServiceImpl implements IWwebchatService{

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private WwebchatMapper wwebchatMapper;

    @Override
    public List<Wwebchat> selectByWwOrWebchat(Wwebchat wwebchat) {
        return wwebchatMapper.selectByWwOrWebchat(wwebchat);
    }

    @Override
    public int insertWwebchat(Wwebchat wwebchat) {
        return wwebchatMapper.insertWwebchat(wwebchat);
    }

    @Override
    public String importWwebchat(List<Wwebchat> wwebchatList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(wwebchatList) || wwebchatList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String operName = ShiroUtils.getLoginName();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (Wwebchat wwebchat : wwebchatList)
        {
            try
            {
                // 验证是否存在这个用户
//                User u = userMapper.selectUserByLoginName(user.getLoginName());
//                if (StringUtils.isNull(u))
//                {
//                    user.setPassword(password);
//                    user.setCreateBy(operName);
                    this.insertWwebchat(wwebchat);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、信息导入成功");
//                }
//                else if (isUpdateSupport)
//                {
//                    user.setUpdateBy(operName);
//                    this.updateUser(user);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
//                }
//                else
//                {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
//                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、信息"+wwebchat.getWwNum()+"导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
